package patrol.mobile.com.mobilepatrol.adapter;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import patrol.mobile.com.mobilepatrol.Item;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.bean.MingxiMoBanBean;

public class XjMingxiAdapter extends RecyclerView.Adapter<XjMingxiAdapter.ViewHolder> {
    private MingxiMoBanBean mingxiMoBanBean;

    public XjMingxiAdapter(MingxiMoBanBean mingxiMoBanBean) {
        this.mingxiMoBanBean = mingxiMoBanBean;
    }

    @Override
    public XjMingxiAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_xj_mingxi_layout, parent, false);
        return new ViewHolder(view);
    }

    public void notifyDataSetChanged(MingxiMoBanBean mingxiMoBanBean) {
        this.mingxiMoBanBean = mingxiMoBanBean;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(XjMingxiAdapter.ViewHolder holder, final int position) {
//        holder.xiangText.setText(mingxiMoBanBean.getList().get(position).getEquipmentName());
//        holder.titleText.setText(mingxiMoBanBean.getList().get(position).getPosition());
//        holder.typeText.setText(mingxiMoBanBean.getList().get(position).getEquipmentStatusText());
//        holder.typeText.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (mingxiMoBanBean.getList().get(position).getEquipmentStatus().equals("0")) {
//                    mingxiMoBanBean.getList().get(position).setEquipmentStatus("1");
//                } else {
//                    mingxiMoBanBean.getList().get(position).setEquipmentStatus("0");
//                }
//                notifyItemChanged(position);
//            }
//        });
//
//        holder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                onItemClickListener.onItemClick(v, position);
//            }
//        });

        final MingxiMoBanBean.ParentBean parentBean = mingxiMoBanBean.getParentBeanList().get(position);
        holder.titleText.setText(parentBean.getProLarge());
//            holder.typeText.setText(shebeiXunJianBean.getJiankkElefailinsprecDivList().get(position).getFaultSituationText());
//            holder.xiangText.setText(shebeiXunJianBean.getJiankkElefailinsprecDivList().get(position).getEquipmentName());
//            holder.tv_text.setText(shebeiXunJianBean.getJiankkElefailinsprecDivList().get(position).getEquipmentName());
        holder.tv_num.setText(position + 1 + "");

        MyChildAdapter myChildAdapter = new MyChildAdapter(parentBean, new Item() {
            @Override
            public int getParentPosition(int p) {
                int pos = mingxiMoBanBean.getList().indexOf(parentBean.getChildList().get(p));
                return pos;
            }
        });
        holder.rv_child.setVisibility(View.VISIBLE);
        holder.rv_child.setAdapter(myChildAdapter);
        holder.rv_child.setLayoutManager(new LinearLayoutManager(holder.rv_child.getContext()));
        holder.re_spand.setVisibility(View.GONE);
        holder.iv_arrow.setImageResource(R.mipmap.arrow_d);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holder.re_spand.getVisibility() == View.VISIBLE) {
                    holder.re_spand.setVisibility(View.GONE);
                    holder.iv_arrow.setImageResource(R.mipmap.arrow_d);
                } else {
                    holder.re_spand.setVisibility(View.VISIBLE);
                    holder.iv_arrow.setImageResource(R.mipmap.arrow_u);
                }
            }
        });
    }

    public void setChildAdapterItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }

    OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return mingxiMoBanBean==null?0:mingxiMoBanBean.getParentBeanList().size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.titleText)
        TextView titleText;
//        @BindView(R.id.xiangText)
//        TextView xiangText;
        @BindView(R.id.typeText)
        TextView typeText;
        @BindView(R.id.itemRecyclerView)
        RecyclerView rv_child;
        @BindView(R.id.re_spand)
        RelativeLayout re_spand;
        @BindView(R.id.tv_num)
        TextView tv_num;
        @BindView(R.id.iv_arrow)
        ImageView iv_arrow;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public class MyChildAdapter extends RecyclerView.Adapter<MyChildAdapter.ViewHolder> {
        MingxiMoBanBean.ParentBean parentBean;
        Item item;

        public MyChildAdapter(MingxiMoBanBean.ParentBean parent, Item item) {
            this.parentBean = parent;
            this.item = item;
        }

        @Override
        public MyChildAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_jdmx_detail_child, parent, false);
            return new MyChildAdapter.ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MyChildAdapter.ViewHolder holder, int position) {
            holder.tv_text.setText(parentBean.getChildList().get(position).getEquipmentName());
            holder.tv_zhuangtai.setText(parentBean.getChildList().get(position).getEquipmentStatusText());
            holder.tv_zhuangtai.setOnClickListener(v -> {
                if (parentBean.getChildList().get(position).getEquipmentStatus().equals("0")) {
                    parentBean.getChildList().get(position).setEquipmentStatus("1");
                } else {
                    parentBean.getChildList().get(position).setEquipmentStatus("0");
                }
                notifyItemChanged(position);
            });
            holder.itemView.setOnClickListener(v -> onItemClickListener.onItemClick(v, item.getParentPosition(position)));
        }

        @Override
        public int getItemCount() {
            return parentBean.getChildList().size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.tv_zhuangtai)
            TextView tv_zhuangtai;
            @BindView(R.id.tv_title_child)
            TextView tv_text;

            ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }
        }
    }
//    public class MyChildAdapter extends RecyclerView.Adapter<MyChildAdapter.ViewHolder> {
//        MingxiMoBanBean.ParentBean parentBean;
//        Item item;
//
//        public MyChildAdapter(MingxiMoBanBean.ParentBean parent, Item item) {
//            this.parentBean = parent;
//            this.item = item;
//        }
//
//        @Override
//        public MyChildAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_jdmx_detail_child, parent, false);
//            return new MyChildAdapter.ViewHolder(view);
//        }
//
//        @Override
//        public void onBindViewHolder(MyChildAdapter.ViewHolder holder, int position) {
//            holder.tv_text.setText(parentBean.getChildList().get(position).getEquipmentName());
//            holder.tv_zhuangtai.setText(parentBean.getChildList().get(position).getEquipmentStatus());
//            holder.itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    startActivityForResult(new Intent(JdxjDetailActivity.this, XjMingXiMuShowDetailActivity.class)
//                                    .putExtra("data", shebeiXunJianBean)
//                                    .putExtra("position", item.getParentPosition())
//                            , Activity.RESULT_CANCELED);
//                }
//            });
//        }
//
//        @Override
//        public int getItemCount() {
//            return parentBean.getChildList().size();
//        }
//
//        public class ViewHolder extends RecyclerView.ViewHolder {
//            @BindView(R.id.tv_zhuangtai)
//            TextView tv_zhuangtai;
//            @BindView(R.id.tv_title_child)
//            TextView tv_text;
//
//            ViewHolder(View view) {
//                super(view);
//                ButterKnife.bind(this, view);
//            }
//        }
//    }
}
